<?php


namespace app\admin\controller\general;


use app\common\controller\Backend;
use think\facade\Db;

class Datavuale extends Backend
{
    protected $datatype = null;
    public function initialize(){
        parent::initialize();
        $this->model = new \app\common\model\DatatypeValue();
        $this -> datatype = \app\common\model\DatatypeValue::getMimetypeList();
        $this ->assign("datatypeList",$this -> datatype );
    }
    public function index(){
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $mimetypeQuery = [];
            $filter = $this->request->request('filter');
            $filterArr = (array) json_decode($filter, true);
            if (isset($filterArr['mimetype']) && stripos($filterArr['mimetype'], ',') !== false) {
                $this->request->get(['filter' => json_encode(array_merge($filterArr, ['mimetype' => '']))]);
                $mimetypeQuery = function ($query) use ($filterArr) {
                    $mimetypeArr = explode(',', $filterArr['mimetype']);
                    foreach ($mimetypeArr as $index => $item) {
                        $query->whereOr('mimetype', 'like', '%'.$item.'%');
                    }
                };
            }

            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $total = $this->model
                ->where($mimetypeQuery)
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($mimetypeQuery)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
//            $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->domain());
//            foreach ($list as $k => &$v) {
//                $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']).$v['url'];
//            }
//            unset($v);
            $result = ['total' => $total, 'rows' => $list];
            return json($result);
        }
        return $this->fetch();
    }

    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if($params){
                $params['datatypename'] = $this -> datatype[$params['datatype']];
                $params['createtime'] = time();
                $result = $this->model->save($params);
                if ($result) {
                    $this->success();
                } else {
                    $this->error();
                }
            }
            $this->error();
        }
        return $this->fetch();
    }


    public function edit($ids=null){
        $row = $this->model->find($ids);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if($params){
                $params['datatypename'] = $this -> datatype[$params['datatype']];
                $params['updatetime'] = time();
//                dd222($params);
                $row->save($params);
            }
            $this->success();
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * 删除类目
     * @param string $ids
     * @throws \Exception
     */
    public function del ($ids = '')
    {
        if ($ids) {
            $this->model->destroy($ids);
//            $this->model -> where('doctorid',$ids) -> delete();
            $this->success();
        }
        $this->error();
    }
}